package com.xiaobaibai.server.comment;

import com.xiaobaibai.dao.CommentChildrenDao;
import com.xiaobaibai.dao.CommentDao;
import com.xiaobaibai.pojo.Comment;
import com.xiaobaibai.pojo.CommentChildren;
import com.xiaobaibai.units.CommentSort;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CommentServiceImpl implements ICommentService{

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private CommentChildrenDao commentChildrenDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<Comment> getCommentList() {

        /**
         * 根据留言的时间顺序排序
         * 1.有子评论就看子评论的留言
         * 2.开始排序
         * 技巧:为每个评论设置一个Map,里面是父评论对应其最后时间
         *      然后根据上方Map来得出顺序,顺序加入List后即可
         */
        Map<Integer, Date> dateMap=new HashMap<>();

        //获取全部父评论+获取全部子评论,两者组合
        List<Comment> commentList = commentDao.getCommentList();
        List<CommentChildren> commentChildrenList = commentChildrenDao.getCommentChildrenList();

        Map<Integer,List<CommentChildren>> map=new HashMap<>();

        for (CommentChildren commentChildren : commentChildrenList) {
            List<CommentChildren> c = map.get(commentChildren.getPId());

            Date date = dateMap.get(commentChildren.getPId());
            if(date==null){
                dateMap.put(commentChildren.getPId(),commentChildren.getCareteTime());
            }else{
                if(commentChildren.getCareteTime().after(date)){
                    //本次时间在取出时间的之后
                    dateMap.put(commentChildren.getPId(),commentChildren.getCareteTime());
                }
                //什么都不做
            }

            if(c==null){
                //首次
                c=new ArrayList<>();
                c.add(commentChildren);
                map.put(commentChildren.getPId(),c);
            }else{
                //非首次
                c.add(commentChildren);
            }
        }

        //先取出并删除未读的父评论id(redis中为set)
        Set<String> newComment = stringRedisTemplate.opsForSet().members("newComment");
        //取完就删掉
        stringRedisTemplate.delete("newComment");

        for (Comment comment : commentList) {

            if(newComment!=null&&newComment.contains(comment.getId().toString()))
                comment.setStatus(true);

            //computeIfAbsent->如果集合中此key为空,则存入
            dateMap.computeIfAbsent(comment.getId(), k -> comment.getCreateTime());
            //子评论必晚于父评论

            List<CommentChildren> ac = map.get(comment.getId());
            comment.setList(ac);
        }

        //实现Comparator接口来排序
        commentList.sort(new CommentSort(dateMap));

        return commentList;
    }

    @Override
    public void delComment(Integer id) {
        commentDao.deleteByPrimaryKey(id);
    }

    @Override
    public void delCommentChildren(Integer id) {
        commentChildrenDao.deleteByPrimaryKey(id);
    }

    @Override
    public void addCommentChildren(CommentChildren commentChildren) {
        commentChildrenDao.insertSelective(commentChildren);
    }

}
